from pymongo import MongoClient
from pymongo.errors import ConnectionFailure

# MongoDB连接配置
MONGO_HOST = "101.227.69.34"
MONGO_PORT = 2929
MONGO_USER = "admin"
MONGO_PASS = "admin"
try:
    # 创建MongoDB客户端连接
    client = MongoClient(
        host=MONGO_HOST,
        port=MONGO_PORT,
        username=MONGO_USER,
        password=MONGO_PASS,
        authSource="admin"  # 认证数据库
    )
    # 测试连接
    client.admin.command("ping")
    print("MongoDB连接成功！")
    # 可以添加更多测试代码，比如列出数据库
    print("数据库列表:", client.list_database_names())

except ConnectionFailure as e:
    print(f"MongoDB连接失败: {e}")
except Exception as e:
    print(f"发生错误: {e}")
finally:
    # 关闭连接
    if "client" in locals():
        client.close()